package com.lnsoft.cac.server.handle.codec.frameMessage;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;
import lombok.NonNull;

@Data
/**
 * 心跳数据报--确认报文
 * */
public class Heart_Cfm {
    private int Command_Status;         //1byte     数据发送状态：①0xFF成功②0x00失败
    private int Clocktime_Stamp;        //4byte     CAC当前时间

    public ByteBuf toByteBuf() throws Exception {
        if (Command_Status != (byte)0xFF && Command_Status != (byte)0x00) {
            throw new Exception("数据发送状态 设置异常");
        }
        if (Clocktime_Stamp <= 0) {
            throw new Exception("CAC当前时间 设置异常");
        }
        ByteBuf bf = Unpooled.buffer(5);
        bf.writeByte(Command_Status);
        bf.writeIntLE(Clocktime_Stamp);
        return bf;
    }


    //  解析 [心跳数据报-确认] 帧报文内容区 到消息体中
    public static Heart_Cfm decFrameBody(@NonNull byte[] frameBody) throws Exception {
        ByteBuf bf = Unpooled.wrappedBuffer(frameBody);
        Heart_Cfm msg = new Heart_Cfm();
        int len = frameBody.length;
        if (len == 0) {
            return null;
        }
        if (5 != len) {
            throw new Exception("frameBody 数据异常");
        }
        msg.setCommand_Status(bf.getUnsignedByte(0));
        msg.setClocktime_Stamp(bf.getIntLE(1) & 0xFFFFFFFF);
        return msg;
    }
}
